//
//  IFAudioView.m
//  IfengNews
//
//  Created by lihongdan on 14-7-9.
//
//

#import "IFAudioView.h"
#import "BlockUI.h"
#import "UIView+Utils.h"


@implementation IFAudioView

- (instancetype)init{
    self=[super init];
    if(self){
        self.pause=YES;
        self.userInteractionEnabled=YES;
        self.frame=CGRectMake(0, 0, GlobleWidth, 64.f);
       // self.backgroundColor=[UIColor blueColor];
        [self setup];
        
        self.callCenter=[[CTCallCenter alloc] init];
        __weak typeof(self) me=self;
        self.callCenter.callEventHandler=^(CTCall *call){
            
        
            if(call.callState ==CTCallStateIncoming || call.callState==CTCallStateDialing){
                IFAudioPlayerManager *ap=[IFAudioPlayerManager shareInstance];
                if([ap isPlaying]){
               
                    [ap pause];
                    sgrSafeMainThread(^{
                                      [me.playButton setImage:[UIImage imageNamed:@"doc_audio_play.png"]
                                                     forState:UIControlStateNormal];
                                      me.pause=YES;
                    }
                    );
                    
                }
            }else if (call.callState==CTCallStateDisconnected){
                
            }
        };
        
    }
    return self;
}

- (void)setup{
    self.leftLabel=[UILabel new];
    
   
//    self.leftLabel.font=[UIFont systemFontOfSize:12.f];
//    self.leftLabel.text=@"声音：";
//    self.leftLabel.frame=CGRectMake(13.f, (self.height-self.leftLabel.font.lineHeight)/2.f, 40.f, self.leftLabel.font.lineHeight);
//    [self addSubview:self.leftLabel];
    
    self.leftLogo=[[UIImageView alloc] init];
    self.leftLogo.frame=CGRectMake(13.f, (64.f-22.f)/2.f, 32.f, 22.f);
    self.leftLogo.image=[UIImage imageNamed:@"doc_audio_listner.png"];
    [self addSubview:self.leftLogo];
    
    self.slider=[[UISlider alloc] init];
    self.slider.frame=CGRectMake(self.leftLogo.right+3.f, 34.f, 211.f, 1.f);
    [self addSubview:self.slider];
    
    [self.slider setMaximumTrackImage:[UIImage imageNamed:@"doc_audio_pre_bg.png"]forState:UIControlStateNormal];
    [self.slider setMinimumTrackImage:[UIImage imageNamed:@"doc_audio_fore.png"] forState:UIControlStateNormal];
    
    
    self.currentTime=[UILabel new];
  
    self.currentTime.font=[UIFont systemFontOfSize:8.f];
    self.currentTime.frame=CGRectMake(self.slider.left+2, self.slider.bottom+4, 30.f, self.currentTime.font.lineHeight);
    [self addSubview:self.currentTime];
    
    self.totalTime=[UILabel new];
    self.totalTime.font=self.currentTime.font;
    self.totalTime.textAlignment=NSTextAlignmentRight;
    self.totalTime.frame=CGRectMake(self.slider.right-32.f, self.slider.bottom+4, 30.f, self.currentTime.font.lineHeight);
    [self addSubview:self.totalTime];
    
    self.playButton=[UIButton buttonWithType:UIButtonTypeCustom];
    self.playButton.frame=CGRectMake(self.slider.right+12.f, 20.f, 28.f, 28.f);
   // self.playButton.frame=CGRectMake(0, 0, GlobleWidth, 64.f);
    __weak typeof(self) me=self;
    [self.playButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender){
        [me clickPlay];
        
    }];
    [self addSubview:self.playButton];
    
    [self.slider handleControlEvent:UIControlEventTouchDragInside withBlock:^(id sender){
    
        me.isSlider=YES;
        
    }];
    
    [self.slider handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender){
        
        me.isSlider=NO;
        if([IFAudioPlayerManager shareInstance].isPlaying){
            [[IFAudioPlayerManager shareInstance] seekToTime:me.slider.value];
            self.currentTime.text=[me formatSecond:me.slider.value];
        }
        ;
        
    }];
    
    [self updateCss];
    
    
//            UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//    button.backgroundColor=[UIColor orangeColor];
//            button.frame=CGRectMake(0, 0, GlobleWidth, 63.f);
//            [self addSubview:button];
//            [button handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
//                NSLog(@"-------1");
//            }];
}

- (NSString *)formatSecond:(float)second{
    int x=(int)second;
    int miu=x/60;
    int sec=x%60;
    
    return [NSString stringWithFormat:@"%d:%@%d",miu,(sec>9)?@"":@"0",sec];
}



- (void)clickPlay{
    IFAudioPlayerManager *ap=[IFAudioPlayerManager shareInstance];
//    if(![self.audioUrl isEqualToString:ap.audioUrl]){
//        [ap stop];
//        [ap playWithURLString:self.audioUrl];
//        ap.delegate=self;
//        [self.playButton setImage:[UIImage imageNamed:ICurrentTheme.globleStyle.audioPause]
//                         forState:UIControlStateNormal];
//        self.pause=NO;
//
//    }else
    
    if(![ap isPlaying]){
        [ap playWithURLString:self.audioUrl];
        ap.delegate=self;
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_pause.png"]
                       forState:UIControlStateNormal];
        self.pause=NO;
    }else if([ap isPause]){
        [ap resume];
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_pause.png"]
                         forState:UIControlStateNormal];
        self.pause=NO;
        
    }else if([ap isStop]) {
        [ap play];
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_pause.png"]
                         forState:UIControlStateNormal];
        self.pause=NO;
    
    }else{
        [ap pause];
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_play.png"]
                         forState:UIControlStateNormal];
        self.pause=YES;
    }


}

- (void)playingStstusTotalTime:(double) total current:(double)current{
    self.slider.maximumValue=total;
    self.totalTime.text=[self formatSecond:total];
    self.slider.minimumValue=0.f;
    self.currentTime.text=[self formatSecond:current];
    [self.slider setValue:current animated:NO];
}

- (void)audioProgerss:(float)progerss{
    if(self.isSlider) return;
    [self.slider setValue:progerss animated:YES];
    //self.currentTime.text=[NSString stringWithFormat:@"%.1f",progerss];
    self.currentTime.text=[self formatSecond:progerss];
}



- (void)updateCss{
    self.leftLabel.textColor=RGB(0x55, 0x57, 0x5e);
   // self.leftLogo.image=[UIImage imageNamed:ICurrentTheme.globleStyle.audiologo];
    self.currentTime.textColor=self.totalTime.textColor=RGB(0x55, 0x57, 0x5e);
    if(!self.pause)
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_pause.png"]
                         forState:UIControlStateNormal];
        else
            [self.playButton setImage:[UIImage imageNamed:@"doc_audio_play.png"]
                             forState:UIControlStateNormal];
    [self.slider setThumbImage:[UIImage imageNamed:@"doc_audio_point.png"] forState:UIControlStateNormal];
    
    
    
    [self.slider setMinimumTrackTintColor:[UIColor redColor]];
//    [self.slider setMinimumTrackImage:[UIImage imageNamed:ICurrentTheme.globleStyle.audioFore] forState:UIControlStateNormal];doc_audio_play.png
//    [self.slider setMaximumTrackImage:[UIImage imageNamed:ICurrentTheme.globleStyle.audioBg] forState:UIControlStateNormal];
    
}

- (void)checkStatus{
    IFAudioPlayerManager *ap=[IFAudioPlayerManager shareInstance];
    ap.delegate=self;
    
    self.slider.maximumValue=ap.player.duration;
    self.totalTime.text=[self formatSecond:ap.player.duration];
    self.slider.minimumValue=0.f;
    [self.slider setValue:ap.player.progress animated:NO];
    self.currentTime.text=[self formatSecond:ap.player.progress];
    
    if(![ap isPause]){
     
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_pause.png"]
                         forState:UIControlStateNormal];
        self.pause=NO;
        
    }else{
      
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_play.png"]
                         forState:UIControlStateNormal];
        self.pause=YES;
    }

}

- (void)audioRemoteControlReceivedWithEvent:(UIEvent *)event{
    IFAudioPlayerManager *ap=[IFAudioPlayerManager shareInstance];
    if([ap isPause]){
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_play.png"]
                         forState:UIControlStateNormal];
        self.pause=YES;
    }else{
        [self.playButton setImage:[UIImage imageNamed:@"doc_audio_pause.png"]
                         forState:UIControlStateNormal];
        self.pause=NO;
    }

}

- (void)finishwithReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration{
    [self.playButton setImage:[UIImage imageNamed:@"doc_audio_play.png"]
                     forState:UIControlStateNormal];
    self.pause=YES;
}




@end
